package net.expectx.pay.controller;

import io.swagger.annotations.ApiOperation;
import net.expectx.common.single.base.BaseResult;
import net.expectx.pay.common.AdminConstants;
import net.expectx.pay.common.AdminResult;
import net.expectx.pay.common.util.ExpectXUtil;
import net.expectx.pay.dao.bean.*;
import net.expectx.pay.dao.mapper.PaymentChannelMapper;
import net.expectx.pay.rpc.api.PaymentChannelService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author hxs
 */
@Controller
@RequestMapping(value = "/manager/system/paymentChannel")
public class PaymentChannelController {

    @Autowired
    private PaymentChannelService paymentChannelService;
    
    
    @ApiOperation(value = "支付渠道主页")
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
        return "paymentChannel/index";
    }

    @ApiOperation(value = "新增支付渠道")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create() {
        return "paymentChannel/modify";
    }

    @ApiOperation(value = "修改支付渠道")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    public String update(@PathVariable("id") Long id, ModelMap modelMap) {
        PaymentChannel paymentChannel = paymentChannelService.selectByPrimaryKey(id);
        modelMap.put("paymentChannel", paymentChannel);
        return "paymentChannel/modify";
    }

    @RequestMapping(value = "/page", method = RequestMethod.GET)
    @ResponseBody
    public Object page(
            @RequestParam(required = false, defaultValue = "1", value = "page") int page,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>(16);

        String paymentChannelName = request.getParameter("paymentChannelName");
        PaymentChannelExample paymentChannelExample = new PaymentChannelExample();
        /**
         * 创造条件
         */
        PaymentChannelExample.Criteria criteria=paymentChannelExample.createCriteria();
        criteria.andIsDeletedEqualTo(AdminConstants.NO);
        if (StringUtils.isNotBlank(paymentChannelName)) {
            criteria.andNameLike(paymentChannelName+"%");
        }
        /**
         * 分页
         */
        paymentChannelExample.setPageIndex(page);
        paymentChannelExample.setPageSize(limit);
        /**
         * 排序
         */
        paymentChannelExample.setOrderByClause("gmt_modified desc");

        List<PaymentChannel> paymentChannels = paymentChannelService.selectByExample(paymentChannelExample);
        long total = paymentChannelService.countByExample(paymentChannelExample);

        result.put("code", AdminConstants.LAYUI_DATA_OK);
        result.put("count", total);
        result.put("msg", "");
        result.put("data", paymentChannels);
        return result;
    }

    @ApiOperation(value = "新增应用渠道")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Object create(PaymentChannel paymentChannel) {
        paymentChannel.setGmtCreate(System.currentTimeMillis());
        paymentChannel.setGmtModified(System.currentTimeMillis());
        paymentChannel.setIsDeleted((byte) 0);
        return paymentChannelService.insertSelective(paymentChannel);
    }

    @ApiOperation(value = "修改应用渠道")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Object update(@PathVariable("id") Long id, PaymentChannel paymentChannel) {

        paymentChannel.setGmtModified(System.currentTimeMillis());
        return  paymentChannelService.updateByPrimaryKeySelective(paymentChannel);
    }

    @ApiOperation(value = "删除应用渠道")
    @RequestMapping(value = "/physicalRemove/{ids}", method = RequestMethod.GET)
    @ResponseBody
    public Object physicalRemove(@PathVariable("ids") String ids) {
        PaymentChannelExample paymentChannelExample = new PaymentChannelExample();
        paymentChannelExample.createCriteria().andIdIn(ExpectXUtil.toList(ids,","));
        BaseResult baseResult = paymentChannelService.physicalRemoveByExample(AdminConstants.YES, paymentChannelExample);
        return new AdminResult(baseResult);
    }
    @ApiOperation(value = "置顶")
    @RequestMapping(value = "/order/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Object update(@PathVariable("id") Long id) {
        PaymentChannel paymentChannel = new PaymentChannel();
        paymentChannel.setId(id);
        paymentChannel.setOrders(System.currentTimeMillis());
        paymentChannel.setGmtModified(System.currentTimeMillis());
        return  paymentChannelService.updateByPrimaryKeySelective(paymentChannel);
    }
}
